package org.igeek.atomqq.task;

import java.net.URL;
import java.util.Date;

import org.igeek.atomqq.activity.BaseActivity;
import org.igeek.atomqq.domain.FriendInfo;
import org.igeek.atomqq.net.HttpConnection;
import org.igeek.atomqq.net.HttpConnection.Request_TYPE;
import org.igeek.atomqq.net.Response;
import org.igeek.atomqq.util.URLManager;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


/**
 * 获取好友信息
 * 正常返回类型: application/json
 * 
 * GET application/json	http://s.web2.qq.com/api/get_friend_info2?tuin=qq号&verifysession=&code=&vfwebqq=令牌&t=1328346730147时间	
 * @author hangxin1940@gmail.com
 * @version 创建时间：2012-1-29 下午1:32:26
 * 
 */
public class FriendInfoTask extends CachedAsyncTask<Void,Void,Void>{
	public static final int ERROR=0x5020;
	
	private TaskResultListener listener;
	private FriendInfoResponse response;
	private long uin;
	private int flag;
	
	private Handler handler=new Handler(){
		
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ERROR:
				activity.showDialog(ERROR, msg.getData());
				break;
			default:
				activity.showDialog(BaseActivity.DIALOG_ERROR, msg.getData());
				break;
			}
		}
	};
	
	private BaseActivity activity;
	
	public FriendInfoTask(BaseActivity activity){
		this.activity=activity;
	}
	
	public void setOnTaskResult(TaskResultListener listener,int flag){
		this.listener=listener;
		this.flag=flag;
	}
	
	public void setParams(long uin){
		this.uin=uin;
	}

	@Override
	protected Void doInBackground(Void... params) {
		String vfwebqq=HttpConnection.getCookieManager().getCookieValue("vfwebqq");
		
		StringBuffer surl=new StringBuffer(URLManager.FRIEND_INFO);
		surl.append("?verifysession=&code=&tuin=");
		surl.append(uin);
		surl.append("&vfwebqq=");
		surl.append(vfwebqq);
		surl.append("&t=");
		surl.append(new Date().getTime());
		try {
			URL url = new URL(surl.toString());
			
			HttpConnection conn=new HttpConnection(url, Request_TYPE.GET);
			conn.setReferer(URLManager.REFER_s_web2_qq_com_proxy);
			conn.connect();
			response=new FriendInfoResponse();
			response.setFriendInfo(conn.getResponse().getJsonObj());
			conn.disconnect();
			
			
			
		}catch (Exception e) {
			e.printStackTrace();
			showErrorDialog("网络异常",ERROR);
		}
		
		
		return null;
	}

	
	
	private void showErrorDialog(String message,int flag){
		Bundle bundle=new Bundle();
		bundle.putString(BaseActivity.FLAG_ERROR, message);
		Message msg=new Message();
		msg.setData(bundle);
		msg.what=flag;
		handler.sendMessage(msg );
	}
	
	@Override
	protected void onPostExecute(Void result) {
		if(null!=response)
			listener.onTaskResult(response,flag);
	}	

	
	
	public class FriendInfoResponse extends Response{
		private FriendInfo friendInfo;

		public FriendInfo getFriendInfo() {
			return friendInfo;
		}

		public void setFriendInfo(JSONObject jInfo) throws JSONException {
			JSONObject res=jInfo.getJSONObject("result");
			
			friendInfo=new FriendInfo(uin);
			
			friendInfo.setOccupation(res.getString("occupation"));
			friendInfo.setPhone(res.getString("phone"));
			friendInfo.setAllow(res.getInt("allow"));
			friendInfo.setCollege(res.getString("college"));
			friendInfo.setConstel(res.getInt("constel"));
			friendInfo.setBlood(res.getInt("blood"));
			friendInfo.setHomepage(res.getString("homepage"));
			friendInfo.setStat(res.getInt("stat"));
			friendInfo.setVip_info(res.getInt("vip_info"));
			friendInfo.setCountry(res.getString("country"));
			friendInfo.setCity(res.getString("city"));
			friendInfo.setPersonal(res.getString("personal"));
			friendInfo.setNick(res.getString("nick"));
			friendInfo.setShengxiao(res.getInt("shengxiao"));
			friendInfo.setEmail(res.getString("email"));
			friendInfo.setClient_type(res.getInt("client_type"));
			friendInfo.setProvince(res.getString("province"));
			friendInfo.setGender(res.getString("gender"));
			friendInfo.setMobile(res.getString("mobile"));
			
			JSONObject birthday=res.getJSONObject("birthday");
			String mon=Integer.toString(birthday.getInt("month"));
			String day=Integer.toString(birthday.getInt("day"));
			String year=Integer.toString(birthday.getInt("year"));
			
			String date =year+"-"+mon+"-"+day;
			friendInfo.setBirthday(date);
		}
		
		
	}

}
